// 1.引入小程序中的axios
import request from "../../utils/request.js"
// 2.创建vuex中核心概念
// 数据存放的位置
const state = {
	navList:[],
	recommendList:[],
	indexGoodsList:[]
}
// 操作数据的mutations
const mutations ={
	// state就是仓库中的state，payload是actions方法中传递的载荷
	SET_NAVLIST(state,payload){
		state.navList = payload
	},
	SET_RECOMMENDLIST(state,payload){
		state.recommendList = payload
	},
	SET_INDEXGOODSLIST(state,payload){
		state.indexGoodsList = payload
	},
}
// 可以异步发送方法的actions
const actions = {
	// 这里本来是上下文context 相当于小型的store
	async getNavlist({commit}){
		const res = await request("/getIndexImgNav")
		// console.log(res.itemList)
		commit("SET_NAVLIST",res.itemList)
	},
	async getRecommendList({commit}){
		const res = await request("/getIndexImgNav")
		// console.log(res.recommendList)
		commit("SET_RECOMMENDLIST",res.recommendList)
	},
	async getIndexGoodsList({commit}){
		const res = await request("/getIndexGoodsList")
		// console.log(res)
		commit("SET_INDEXGOODSLIST",res)
	}
}
// 仓库中的计算属性
const getters = {
		rankList(state){
			// console.log(state.indexGoodsList.splice(0,5));
			// console.log(state.indexGoodsList.splice(0,5).map((item)=>{
			// 	return {
			// 		sellPoint:item.sellPoint,
			// 		title:item.title
			// 	}
			// }));
			return state.indexGoodsList.slice(0,4).map((item)=>{
				return {
					sellPoint:item.sellPoint,
					title:item.title
				}
			}) || []
		},
		recList(state){
			// console.log(state.indexGoodsList.slice(0,5).map((item)=>{
			// 	// return	item.recList.slice(0,8)
			// }));
			return state.indexGoodsList.slice(0,5).map((item)=>{
				return	item.recList.slice(0,8)
			}) || []
		}
}
export default{
	namespaced: true,
	state,
	mutations,
	actions,
	getters,
}